<?php

require_once("EKElement.php");
require_once("EKDocument.php");
require_once("EKParagraph.php");
require_once("EKTalk.php");

class EKParagraphRelationship extends EKElement{
	private $id;
	private $updateTime;
	private $enabled;
	private $paragraphIds;
	
	static function getPRXmlNode($id){
		$xmlfile = SERVERLOCATION."data/EKParagraphRelationship.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$node = $doc->getElementById($id);
		return $node;
	}
	
	static function getUpdateTimeById($id){
		$node = EKParagraphRelationship::getPRXmlNode($id);
		return date("Y/m/d[H:i:s]", $node->getElementsByTagName("updateTime")->item(0)->nodeValue);
	}
	
	static function getOwnerIdById($id){
		$node = EKParagraphRelationship::getPRXmlNode($id);
		return $node->getElementsByTagName("ownerId")->item(0)->nodeValue;
	}
	
	static function getParagraphIdsById($id){
		$node = EKParagraphRelationship::getPRXmlNode($id);
		$items = $node->getElementsByTagName("paragraphId");
		for($i=0;$i<$items->length;$i++){
			$paragraphIds[] = $items->item($i)->nodeValue;
		}
		for($i=0;$i<count($paragraphIds);$i++){
			$currentParagraph = new EKParagraph($paragraphIds[$i]);
			if($currentParagraph->getStatus() == "approved"){
				$temp = $paragraphIds[0];
				$paragraphIds[0] = $paragraphIds[$i];
				$paragraphIds[$i] = $temp;
			}
		}
		return $paragraphIds;
	}
	
	static function createNewPRs($ownerId, $pr){
		
		$xmlfile = SERVERLOCATION."data/EKParagraphRelationship.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$PRRoot = $doc->getElementsByTagName("paragraphRelationshipRoot")->item(0);
		
		$pRNode = $doc->createElement("paragraphRelationship");
		$newId = EKStatistics::getParagraphRelationshipLastId();
		$pRNode->setAttribute("id", $newId);
		$pRNode->setAttribute("enabled", "true");
			
		# ownerId, Update Time
		EKParagraphRelationship::giveNewTextNode($doc, "ownerId", $ownerId, $pRNode);
		EKParagraphRelationship::giveNewTextNode($doc, "updateTime", time(), $pRNode);
	
		# paragraphId
		$paragraphId = EKParagraph::createNewParagraphNode($ownerId, $newId, $pr);
		EKParagraphRelationship::giveNewTextNode($doc, "paragraphId", $paragraphId, $pRNode);
			
		$PRRoot->appendChild($pRNode);
		
		$doc->save($xmlfile);
		return $newId;
	}
	
	static function deletePRNode($id, $quick = false){
		$xmlfile = SERVERLOCATION."data/EKParagraphRelationship.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$documentRoot = $doc->getElementsByTagName("paragraphRelationshipRoot")->item(0);
		$thisDoc = $doc->getElementById($id);		
		
		# this function may be called either when the owner is deleted
		# or when the only paragraph of it is deleted
		# In the second case, we should judge the case that it is the only pr in its owner
		if(!$quick){
			$ownerId = EKParagraphRelationship::getOwnerIdById($id);
			$owner = new EKDocument($ownerId);
			if(count($owner->getPRIds())==1)
			{
				$a[] = EKDocument::deleteDocNode($ownerId);
				$a[] = "Document ".$owner->getTitle()." deleted.";
				return $a;
			}
			EKElement::deleteRefIdById($ownerId, $id, "EKDocument", "paragraphRelationshipId");
		}
		
		# delete its paragraphs
		$pa = EKParagraphRelationship::getParagraphIdsById($id);
		if(count($pa)>0)
		{foreach($pa as $p){
			EKParagraph::deleteParagraphNode($p, true);
		}}

		$documentRoot->removeChild($thisDoc);
		EKStatistics::decParagraphRelationshipSumNum();
		$doc->save($xmlfile);
		return false;
	}
	
	
	static function denyPRNode($id){
		$xmlfile = SERVERLOCATION."data/EKParagraphRelationship.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$documentRoot = $doc->getElementsByTagName("paragraphRelationshipRoot")->item(0);
		$thisDoc = $doc->getElementById($id);		
		
		# this function may be called when the only paragraph of it is deleted
		# We should judge the case that it is the only pr in its owner
		$ownerId = EKParagraphRelationship::getOwnerIdById($id);
		$owner = new EKDocument($ownerId);
		if(count($owner->getPRIds())==1)
		{
			return EKDocument::denyDocNode($ownerId);
		}
		EKElement::deleteRefIdById($ownerId, $id, "EKDocument", "paragraphRelationshipId");

		$documentRoot->removeChild($thisDoc);
		EKStatistics::decParagraphRelationshipSumNum();
		$doc->save($xmlfile);
		return false;
	}
	
	# do not check the user session
	# so the session should be checked before it is called
	static function administrate($id, $action){
		
		$xmlfile = SERVERLOCATION."data/EKParagraphRelationship.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$thisDoc = $doc->getElementById($id);
		
		# the enabled attribute for PR is obselete
		
		#if($thisDoc->getAttribute("enabled") == "false" )
		#{
		#	return "The action '".$action."' on PR id = ".$id." is not permitted!";
		#}
		#
		#switch($action)
		#{
		#	case "deny":
		#		$thisDoc->setAttribute("enabled", "false");
		#		break;
		#}
		
		$pIds = EKParagraphRelationship::getParagraphIdsById($id);
		if(count($pIds)>0){
		foreach($pIds as $a){
			EKParagraph::administrate($a, $action, true);
		}}
		
		#$doc->save($xmlfile);

		return false;
	}
	
	static function isApproved($id)
	{
		$pa = EKParagraphRelationship::getParagraphIdsById($id);
		foreach($pa as $a){
			$s = EKElement::getElementAttributeById($a, "EKParagraph", "status");
			if($s == "approved")return true;
		}
		return false;
	}
}
?>